package com.swak.proxy.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * cglib 来代理实现类
 * @author lifeng
 */
public class CglibProxy implements MethodInterceptor {

	public Object object;

	public CglibProxy(Object object) {
		this.object = object;
	}

	/**
	 * 创建代理类
	 * @return
	 */
	public Object getProxy() {
		// 创建加强器
		Enhancer enhancer = new Enhancer();
		// 设置需要加强的类
		enhancer.setSuperclass(object.getClass());
		// 设置回调
		enhancer.setCallback(this);
		enhancer.setClassLoader(object.getClass().getClassLoader());
		return enhancer.create();
	}

	@Override
	public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
		return arg3.invoke(object, arg2);
	}
}
